package com.molyfun.parents.view.tagedittext;


import android.content.Context;
import android.graphics.Canvas;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;

import com.molyfun.parents.R;
import com.molyfun.parents.utils.StringTagUtils;

import java.util.List;

public class TagTextView extends android.support.v7.widget.AppCompatTextView  {
    private List<String> tagList;
    private Context context;

    public TagTextView(Context context) {
        this(context, null);
    }

    public TagTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TagTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
    }

    @Override
    public CharSequence getText() {
        return super.getText();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public void setText(String text, TagClickListener clickListener) {
        this.mTagClickListener = clickListener;
        tagList = StringTagUtils.getHashtags(text.toString());
        if (tagList == null || tagList.size() == 0) {
            super.setText(text);
        } else {
            SpannableString ssb = new SpannableString(text);
            List<String> tags = StringTagUtils.getTextList(text.toString());
            int start = 0;
            int end = 0;
            int position = 0;
            for (int i = 0; i < tags.size(); i++) {
                String tag = tags.get(i);
                start = end;
                end += tag.length();
                if (tag.length() > 2 && tag.startsWith("#") && tag.endsWith("#")) {
                    String s = tag.substring(1,tag.length() - 1);
                    if (tagList.contains(s)){
                        ssb.setSpan(new CustomizedClickableSpan(tag, position), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        position++;
                    }
                }
            }
            super.setText(ssb);
            setMovementMethod(LinkMovementMethod.getInstance());//响应点击事件
        }
    }

    private class CustomizedClickableSpan extends ClickableSpan {
        private String text;
        private int mPosition;

        public CustomizedClickableSpan(String text, int position) {
            super();
            this.text = text;
            this.mPosition = position;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(ContextCompat.getColor(context, R.color.material_blue));
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(View widget) {
            if (mTagClickListener != null) {
                mTagClickListener.tabClick(text.replace("#", ""), mPosition);
            }
        }
    }

    private TagClickListener mTagClickListener;

    public interface TagClickListener {
        void tabClick(String tag, int position);
    }


}
